home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / ui.js < prev    next >
Text File  |  2008-05-17  |  11KB  |  422 lines

  1. /*
  2.     ui.js
  3.  
  4.     Copyright ⌐ 2005, 2006, 2007, 2008  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_ui =
  8. {
  9.     init: function()
  10.     {
  11.     },
  12.  
  13.     show_accessible: function()
  14.     {
  15.         try {
  16.             var mainwnd = document.getElementById("main-window");
  17.             if (mainwnd) {
  18.                 var mode = "normal";
  19.  
  20.                 if (wot_prefs.accessible) {
  21.                     mode = "accessible";
  22.                 }
  23.  
  24.                 if (mainwnd.getAttribute("wot-mode") != mode) {
  25.                     mainwnd.setAttribute("wot-mode", mode);
  26.                     wot_my_session.update(false); /* Update cookies */
  27.                 }
  28.             }
  29.         } catch (e) {
  30.             dump("wot_ui.show_accessible: failed with " + e + "\n");
  31.         }
  32.     },
  33.  
  34.     show_toolbar_button: function(id, before)
  35.     {
  36.         try {
  37.             var nbr = document.getElementById("nav-bar");
  38.  
  39.             if (!nbr || nbr.currentSet.indexOf(id) != -1) {
  40.                 return;
  41.             }
  42.  
  43.             var box = document.getElementById("navigator-toolbox");
  44.  
  45.             if (!box) {
  46.                 return;
  47.             }
  48.  
  49.             var bar = box.firstChild;
  50.  
  51.             while (bar) {
  52.                 if (bar.currentSet && bar.currentSet.indexOf(id) != -1) {
  53.                     return;
  54.                 }
  55.                 bar = bar.nextSibling;
  56.             }
  57.  
  58.             var target = document.getElementById(before);
  59.  
  60.             /* The before element might not exist in the nav-bar */
  61.             var elem = nbr.firstChild;
  62.  
  63.             while (elem) {
  64.                 if (elem == target) {
  65.                     break;
  66.                 }
  67.                 elem = elem.nextSibling;
  68.             }
  69.  
  70.             nbr.insertItem(id, elem, null, false);
  71.             document.persist("nav-bar", "currentset");
  72.         } catch (e) {
  73.             dump("wot_ui.show_toolbar_button: failed with " + e + "\n");
  74.         }
  75.     },
  76.  
  77.     /* Shows or hides user interface elements based on preferences */
  78.     show_elements: function()
  79.     {
  80.         try {
  81.             /* Toolbar */
  82.             if (wot_prefs.create_button) {
  83.                 this.show_toolbar_button("wot-button", "urlbar-container");
  84.             }
  85.  
  86.             /* Accessibility */
  87.             this.show_accessible();
  88.  
  89.             /* Rating components */
  90.             document.getElementById("wot-rating-1").hidden =
  91.                 !wot_prefs.show_application_1;
  92.             document.getElementById("wot-rating-2").hidden =
  93.                 !wot_prefs.show_application_2;
  94.             document.getElementById("wot-rating-4").hidden =
  95.                 !wot_prefs.show_application_4;
  96.         } catch (e) {
  97.             dump("wot_ui.show_elements: failed with " + e + "\n");
  98.         }
  99.     },
  100.  
  101.     update: function(description)
  102.     {
  103.         try {
  104.             wot_commands.update();
  105.             this.update_title(description);
  106.             this.update_rating();
  107.             this.update_testimonies();
  108.             this.update_scorecard();
  109.             this.update_users();
  110.             this.update_message();
  111.             this.update_updates();
  112.         } catch (e) {
  113.             dump("wot_ui.update: failed with " + e + "\n");
  114.         }
  115.     },
  116.  
  117.     update_title: function(description)
  118.     {
  119.         try {
  120.             var title = document.getElementById("wot-title-text");
  121.             if (title) {
  122.                 if (wot_cache.isok(wot_core.hostname)) {
  123.                     title.value = wot_core.hostname;
  124.                     title.setAttribute("status", "target");
  125.                 } else {
  126.                     title.value = description;
  127.                     title.setAttribute("status", "information");
  128.                 }
  129.             }
  130.         } catch (e) {
  131.             dump("wot_ui.update_title: failed with " + e + "\n");
  132.         }
  133.     },
  134.  
  135.     update_rating: function()
  136.     {
  137.         try {
  138.             var cached = wot_cache.isok(wot_core.hostname);
  139.  
  140.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  141.                 var reputation = document.getElementById("wot-rating-" + i +
  142.                                     "-reputation");
  143.                 var confidence = document.getElementById("wot-rating-" + i +
  144.                                     "-confidence");
  145.  
  146.                 var rep = -1, cnf = -1;
  147.                 if (cached) {
  148.                     rep = wot_cache.get(wot_core.hostname, "reputation_" + i);
  149.                     cnf = wot_cache.get(wot_core.hostname, "confidence_" + i);
  150.                 }
  151.  
  152.                 if (reputation) {
  153.                     if (rep >= WOT_MIN_REPUTATION_5) {
  154.                         reputation.setAttribute("reputation", 5);
  155.                     } else if (rep >= WOT_MIN_REPUTATION_4) {
  156.                         reputation.setAttribute("reputation", 4);
  157.                     } else if (rep >= WOT_MIN_REPUTATION_3) {
  158.                         reputation.setAttribute("reputation", 3);
  159.                     } else if (rep >= WOT_MIN_REPUTATION_2) {
  160.                         reputation.setAttribute("reputation", 2);
  161.                     } else if (rep >= 0) {
  162.                         reputation.setAttribute("reputation", 1);
  163.                     } else if (cached) {
  164.                         reputation.setAttribute("reputation", 0);
  165.                     } else {
  166.                         reputation.removeAttribute("reputation");
  167.                     }
  168.                 }
  169.  
  170.                 if (confidence) {
  171.                     if (cnf >= WOT_MIN_CONFIDENCE_5) {
  172.                         confidence.setAttribute("confidence", 5);
  173.                     } else if (cnf >= WOT_MIN_CONFIDENCE_4) {
  174.                         confidence.setAttribute("confidence", 4);
  175.                     } else if (cnf >= WOT_MIN_CONFIDENCE_3) {
  176.                         confidence.setAttribute("confidence", 3);
  177.                     } else if (cnf >= WOT_MIN_CONFIDENCE_2) {
  178.                         confidence.setAttribute("confidence", 2);
  179.                     } else if (cnf >= WOT_MIN_CONFIDENCE_1) {
  180.                         confidence.setAttribute("confidence", 1);
  181.                     } else {
  182.                         confidence.setAttribute("confidence", 0);
  183.                     }
  184.                 }
  185.             }
  186.         } catch (e) {
  187.             dump("wot_ui.update_rating: failed with " + e + "\n");
  188.         }
  189.     },
  190.  
  191.     /* Updates the testimony element */
  192.     update_testimonies: function(hover, pos)
  193.     {
  194.         try {
  195.             var cached = wot_cache.isok(wot_core.hostname);
  196.  
  197.             /* CSS rules */
  198.             var sld_rule = wot_css.getstyle(WOT_STYLESHEET,
  199.                                 ".wot-rating-slider");
  200.  
  201.             if (!sld_rule) {
  202.                 dump("wot_ui.update_testimonies: no style rule?\n");
  203.                 return;
  204.             }
  205.  
  206.             /* Dimensions */
  207.             var sld_w = wot_css.getstyle_numeric(sld_rule, "width");
  208.  
  209.             /* Sliders */
  210.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  211.                 /* Slider elements */
  212.                 var stack     = document.getElementById("wot-rating-" +    i +
  213.                                     "-stack");
  214.                 var indicator = document.getElementById("wot-rating-" +    i +
  215.                                     "-indicator");
  216.  
  217.                 if (!stack || !indicator) {
  218.                     continue;
  219.                 }
  220.  
  221.                 var testimony = -1;
  222.                 if (cached) {
  223.                     testimony = wot_cache.get(wot_core.hostname,
  224.                                 "testimony_" + i);
  225.                 }
  226.  
  227.                 if (testimony >= 0) {
  228.                     indicator.left = testimony * sld_w / WOT_MAX_REPUTATION;
  229.                     if (testimony == WOT_MAX_REPUTATION) {
  230.                         --indicator.left;
  231.                     }
  232.                     stack.setAttribute("testimony", "true");
  233.                 } else if (hover != null && i == hover) {
  234.                     indicator.left = pos * sld_w / WOT_MAX_REPUTATION;
  235.                     if (pos == WOT_MAX_REPUTATION) {
  236.                         --indicator.left;
  237.                     }
  238.                     stack.setAttribute("testimony", "hover");
  239.                 } else {
  240.                     stack.setAttribute("testimony", "false");
  241.                 }
  242.  
  243.                 var help = document.getElementById("wot-rating-" + i +
  244.                             "-help-text");
  245.                 var link = document.getElementById("wot-rating-" + i +
  246.                             "-help-link");
  247.  
  248.                 if (help && link) {
  249.                     if (testimony >= WOT_MIN_REPUTATION_5) {
  250.                         help.value = wot_util.getstring("help_5");
  251.                     } else if (testimony >= WOT_MIN_REPUTATION_4) {
  252.                         help.value = wot_util.getstring("help_4");
  253.                     } else if (testimony >= WOT_MIN_REPUTATION_3) {
  254.                         help.value = wot_util.getstring("help_3");
  255.                     } else if (testimony >= WOT_MIN_REPUTATION_2) {
  256.                         help.value = wot_util.getstring("help_2");
  257.                     } else if (testimony >= 0) {
  258.                         help.value = wot_util.getstring("help_1");
  259.                     } else {
  260.                         help.value = "";
  261.                     }
  262.  
  263.                     link.removeAttribute("help");
  264.                     link.value = "";
  265.  
  266.                     if (cached && testimony >= 0) {
  267.                         var r = wot_cache.get(wot_core.hostname,
  268.                                     "reputation_" + i);
  269.                         if (r != null && r >= 0 &&
  270.                             Math.abs(r - testimony) > WOT_MIN_COMMENT_DIFF) {
  271.                             help.value = wot_util.getstring("help_comment");
  272.                             link.value = wot_util.getstring("help_comment_link");
  273.                             link.setAttribute("comment", "true");
  274.                         }
  275.                     }
  276.  
  277.                     help.hidden = (!help.value || !help.value.length);
  278.                 }
  279.             }
  280.         } catch (e) {
  281.             dump("wot_ui.update_testimonies: failed with " + e + "\n");
  282.         }
  283.     },
  284.  
  285.     update_scorecard: function()
  286.     {
  287.     },
  288.  
  289.     update_message: function()
  290.     {
  291.         try {
  292.             var msg = document.getElementById("wot-message");
  293.             var txt = document.getElementById("wot-message-text");
  294.  
  295.             if (!msg || !txt || !txt.firstChild) {
  296.                 return;
  297.             }
  298.  
  299.             if (wot_api_query.message.length == 0 ||
  300.                     wot_api_query.message_type.length == 0) {
  301.                 msg.hidden = true;
  302.                 txt.firstChild.nodeValue = "";
  303.                 return;
  304.             }
  305.  
  306.             txt.firstChild.nodeValue = wot_api_query.message;
  307.             txt.setAttribute("url-type",
  308.                 wot_api_query.message_url.substring(0,4));
  309.             msg.setAttribute("message-status", wot_api_query.message_type);
  310.             msg.hidden = false;
  311.         } catch (e) {
  312.             dump("wot_ui.update_message: failed with " + e + "\n");
  313.         }
  314.     },
  315.  
  316.     update_users: function()
  317.     {
  318.         try {
  319.             if (!wot_api_query.users) {
  320.                 return;
  321.             }
  322.  
  323.             var i, j = 0;
  324.  
  325.             for (i = 0; i < wot_api_query.users.length; ++i) {
  326.                 var user    = document.getElementById("wot-user-" + j);
  327.                 var content = document.getElementById("wot-user-" + j + "-content");
  328.                 var stack   = document.getElementById("wot-user-" + j + "-stack");
  329.                 var header  = document.getElementById("wot-user-" + j + "-header");
  330.                 var bar     = document.getElementById("wot-user-" + j + "-bar-image");
  331.                 var label   = document.getElementById("wot-user-" + j + "-bar-text");
  332.                 var text    = document.getElementById("wot-user-" + j + "-text");
  333.                 var notice  = document.getElementById("wot-user-" + j + "-notice");
  334.  
  335.                 if (!user || !header || !bar || !label || !text || !notice) {
  336.                     return;
  337.                 }
  338.  
  339.                 if (wot_api_query.users[i].bar &&
  340.                         wot_api_query.users[i].length != null &&
  341.                         wot_api_query.users[i].label) {
  342.                     header.value = wot_api_query.users[i].bar;
  343.                     label.value = wot_api_query.users[i].label;
  344.                     bar.setAttribute("length", wot_api_query.users[i].length);
  345.                     bar.hidden = false;
  346.                 } else {
  347.                     header.value = "";
  348.                     label.value = "";
  349.                     bar.hidden = true;
  350.                 }
  351.  
  352.                 if (wot_api_query.users[i].url) {
  353.                     content.setAttribute("url", wot_api_query.users[i].url);
  354.                 } else {
  355.                     content.removeAttribute("url");
  356.                 }
  357.  
  358.                 if (wot_api_query.users[i].notice) {
  359.                     notice.value = wot_api_query.users[i].notice;
  360.                     notice.hidden = false;
  361.                 } else {
  362.                     notice.hidden = true;
  363.                 }
  364.  
  365.                 if (wot_api_query.users[i].text) {
  366.                     text.value = wot_api_query.users[i].text;
  367.                     user.hidden = false;
  368.                     ++j;
  369.                 } else {
  370.                     text.value = "";
  371.                     user.hidden = true;
  372.                 }
  373.             }
  374.         } catch (e) {
  375.             dump("wot_ui.update_users: failed with " + e + "\n");
  376.         }
  377.     },
  378.  
  379.     update_updates: function()
  380.     {
  381.         try {
  382.             var updbox = document.getElementById("wot-update");
  383.  
  384.             if (!updbox) {
  385.                 return;
  386.             }
  387.  
  388.             updbox.hidden = !wot_update.isavailable();
  389.         } catch (e) {
  390.             dump("wot_ui.update_updates: failed with " + e + "\n");
  391.         }
  392.     },
  393.  
  394.     geticonurl: function(r, size, plain)
  395.     {
  396.         var image = "no_rep_available";
  397.  
  398.         if (r >= WOT_MIN_REPUTATION_5) {
  399.             image = "trusted";
  400.         } else if (r >= WOT_MIN_REPUTATION_4) {
  401.             image = "alright";
  402.         } else if (r >= WOT_MIN_REPUTATION_3) {
  403.             image = "caution";
  404.         } else if (r >= WOT_MIN_REPUTATION_2) {
  405.             image = "not_safe";
  406.         } else if (r >= 0) {
  407.             image = "danger";
  408.         }
  409.  
  410.         var base = "chrome://wot/skin/fusion/";
  411.  
  412.         if (wot_prefs.accessible) {
  413.             base += "accessible/";
  414.         }
  415.  
  416.         return base + size + "_" + size +
  417.                     ((plain) ? "/plain/" : "/") + image + ".png";
  418.     }
  419. };
  420.  
  421. wot_ui.init();
  422.